package com.example.demo2;

import com.example.demo2.manager.CDServerManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@SpringBootApplication(scanBasePackages = "com.example")
public class Demo2Application implements CommandLineRunner {
    private final CDServerManager cdServerManager;

    /**
     * 使用构造函数注入替代字段注入，提高代码的可测试性和可靠性
     * @param cdServerManager
     */
    @Autowired
    public Demo2Application(CDServerManager cdServerManager) {
        this.cdServerManager = cdServerManager;
    }


    public static void main(String[] args) {
        SpringApplication.run(Demo2Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        cdServerManager.listCDLibrary();

        //1 执行eject 弹出
        cdServerManager.ejectCd();
        //2 执行load 加载CD
        cdServerManager.loadCdToPlayer(0);
        //3 执行play 播放CD
        cdServerManager.printsCDState();
        cdServerManager.playCd();

        // 创建调度器
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        long initialDelay = 5; // 初始播放时间(秒)
        long operationInterval = 5; // 每个操作间隔时间(秒)

        // 播放5s后执行快进逻辑
        scheduler.schedule(() -> {
            System.out.println("播放5秒后，执行【快进-播放】...");
            cdServerManager.forwardCd();
            cdServerManager.playCd();
            cdServerManager.printsCDState();
            // 播放5秒后执行回退逻辑
            scheduler.schedule(() -> {
                System.out.println("再次播放5秒后，执行【回退-播放】...");
                cdServerManager.backCd();
                cdServerManager.playCd();
                cdServerManager.printsCDState();
                // 播放5秒后执行暂停
                scheduler.schedule(() -> {
                    System.out.println("播放5秒后，执行暂停...");
                    cdServerManager.pauseCd();
                    cdServerManager.printsCDState();
                    // 暂停5秒后继续播放
                    scheduler.schedule(() -> {
                        System.out.println("暂停5秒后，继续播放...");
                        cdServerManager.playCd();

                        // 播放5秒后执行停止
                        scheduler.schedule(() -> {
                            System.out.println("播放5秒后，执行停止...");
                            cdServerManager.stopCd();
                            cdServerManager.ejectCd();
                            cdServerManager.printsCDState();
                            // 关闭调度器
                            scheduler.shutdown();

                            System.exit(0);
                        }, operationInterval, TimeUnit.SECONDS);
                    }, operationInterval, TimeUnit.SECONDS);
                }, operationInterval, TimeUnit.SECONDS);

            }, operationInterval, TimeUnit.SECONDS);

        }, initialDelay, TimeUnit.SECONDS);

        System.in.read();
    }
}
